package com.alg;

import java.util.LinkedList;
import java.util.Stack;

/**
 * @author: sun.hongliang
 * @create: 2020/01/16 15:45
 * <p>
 * ["MinStack","push","push","push","push","getMin","pop","getMin","pop","getMin","pop","getMin"]
 * [[],[2],[0],[3],[0],[],[],[],[],[],[],[]]
 * <p>
 * <p>
 * [null,null,null,null,null,0,null,2,null,2,null,2]
 * <p>
 * [null,null,null,null,null,0,null,0,null,0,null,2]
 */
public class MinStack {

    private LinkedList<Integer> list = new LinkedList<>();
    private LinkedList<Integer> minList = new LinkedList<>();

    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        Integer peek = stack.peek();
        System.out.println("p:" + peek);
    }

    public MinStack() {

    }

    public void push(int x) {
        list.addLast(x);
        if (minList.size() == 0 || x <= minList.getLast()) {
            minList.addLast(x);
        }
    }

    public void pop() {
        if (list.size() > 0) {
            Integer listLast = list.removeLast();
            if (listLast.equals(minList.getLast())) {
                minList.removeLast();
            }
        }
    }

    public int top() {
        if (list.size() > 0) {
            return list.getLast();
        }
        return -1;
    }

    public int getMin() {
        if (minList.size() > 0) {
            return minList.getLast();
        }
        return -1;
    }

}
